/******************* 公共变量 *******************/
var local,
	marker,
	gpsDetail,
	lng = 0,
	lat = 0;

/******************* 页面初始化代码 *******************/
$.ajax({
	url: checkNews,
	type: 'post',
	success: function(data) {
		console.log(data);

		common.hintNewMsg(data);
	}
});
$('#map').height(winHeight - $('html body .index-footer').height() - headerTitleHeight);

/******************* 获取当前位置的IP地址 *******************/
// core.bdMap({
// 	selector: 'allmap',
// 	device: 'mb',
// 	ipDetail: function (data) {
// 		data.content.address_component.city
// 	},
// 	gpsDetail: function (data) {
// 		$('body .index .local .local-header a').html(data.result.addressComponent.city.substr(0, data.result.addressComponent.city.length - 1));
// 	}
// });

/******************* 初始化微信 *******************/
wx.ready(function () {
	wx.checkJsApi({
		jsApiList : ['getLocation', 'uploadImage','previewImage','chooseImage','scanQRCode','startRecord','stopRecord','onVoiceRecordEnd','playVoice','onVoicePlayEnd','onMenuShareAppMessage'],  
		success : function(res) {  
		},
		complete: function (data) {
			if (data.checkResult.getLocation == 'no') {
				useGps(true);
			} else {
				useGps();
			}
		}
	});
});

function useGps(useBaidu) {
	function addPoint(x, y) {
		$.ajax({
			url: listUrl,
			type: 'post',
			data: {
				lng: x,
				lat: y
			},
			success: function (data) {
				console.log(data);

				// data[1].list[0].total_money = 188;
				// data[1].list[0].type = '0';
				// data[0].list[0].total_money = 188;
				// data[0].list[0].type = '0';
				// data[0].list[2].total_money = 188;
				// data[0].list[2].type = '0';
				var map = bdMap.map;
				map.clearOverlays();
				// var point = new BMap.Point(lng, lat);		// 使用店铺的百度地图经纬度创建地图定位点
				// map.addOverlay(new BMap.Marker(point));		// 添加当前位置
				var nowPoint = new BMap.Point(x, y);
				map.centerAndZoom(new BMap.Point(nowPoint), 14);		// 创建中心点为当前位置
				map.addOverlay(new BMap.Marker(nowPoint));		// 添加客户位置

				// map.addControl(new BMap.MapTypeControl());	 //添加地图类型控件

				map.enableScrollWheelZoom(true);		 //开启鼠标滚轮缩放
				map.setCenter(nowPoint);
				map.addControl(new BMap.NavigationControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT, type: BMAP_NAVIGATION_CONTROL_SMALL})); 

				//创建自定义marker
				// for (var i = 0; i < data.length; i++) {
				// 	map.addOverlay(new BMap.Marker(new BMap.Point(data[i].lng, data[i].lat),{icon:new BMap.Icon(data[i].photo_id, new BMap.Size(60,60))}));							// 将标注添加到地图中
				// 	$('html body .wrap .activity-list-map #map div div div:nth-child(2) .BMap_Marker').not($('html body .wrap .activity-list-map #map div div div:nth-child(2) .BMap_Marker').eq(0)).append('<a href="' + activityUrl + data[i].id + '" title=""><span>' + data[i].title + '</span><strong>' + data[i].gift + '</strong></a>').on('touchend', function () {
				// 		$(this).hasClass('active') ? $(this).removeClass('active') : $(this).addClass('active').siblings().removeClass('active');
				// 	}).find('a').on('touchend', function () {
				// 		core.href($(this).attr('href'));
				// 	});
				// }
				for (var i in data) {
					// data[i].more = true;
					if (data[i].more) {
						// data[i].list[0].photo_id = 'http://api0.map.bdimg.com/images/marker_red_sprite.png';
						map.addOverlay(new BMap.Marker(new BMap.Point(data[i].list[0].lng, data[i].list[0].lat),{icon:new BMap.Icon(data[i].list[0].photo_id, new BMap.Size(60,60))}));							// 将标注添加到地图中
						$('html body .wrap .activity-list-map #map div div div:nth-child(2) .BMap_Marker').not($('html body .wrap .activity-list-map #map div div div:nth-child(2) .BMap_Marker').eq(0)).eq(i).attr({
							lng: data[i].list[0].lng,
							lat: data[i].list[0].lat
						}).append('<i>' + data[i].list.length + '</i>').on('touchend', function () {
							var $this = $(this),
								nowPoint = new BMap.Point($this.attr('lng'), $this.attr('lat'));
							bdMap.map.setZoom(14);
							map.setCenter(nowPoint);

							$this.hasClass('active') ? $this.removeClass('active') : $this.addClass('active').siblings().removeClass('active');
						}).on('touchend', function (num) {
							return function () {
								core.ls('activityList', 'o', JSON.stringify(data[num].list));
								core.href(activityListUrl);
							}
						}(i));
					} else {
						// data[i].list[0].photo_id = 'http://api0.map.bdimg.com/images/marker_red_sprite.png';
						map.addOverlay(new BMap.Marker(new BMap.Point(data[i].list[0].lng, data[i].list[0].lat),{icon:new BMap.Icon(data[i].list[0].photo_id, new BMap.Size(60,60))}));							// 将标注添加到地图中
						$('html body .wrap .activity-list-map #map div div div:nth-child(2) .BMap_Marker').not($('html body .wrap .activity-list-map #map div div div:nth-child(2) .BMap_Marker').eq(0)).eq(i).attr({
							lng: data[i].list[0].lng,
							lat: data[i].list[0].lat
						}).append('<a href="' + activityUrl + data[i].list[0].id + '" title=""><span>' + data[i].list[0].title + '</span><strong>' + core.decimalRound(Number(data[i].list[0].total_money), 0) + '</strong></a>').on('touchend', function () {
							var $this = $(this),
								nowPoint = new BMap.Point($this.attr('lng'), $this.attr('lat'));
							bdMap.map.setZoom(14);
							map.setCenter(nowPoint);

							$this.hasClass('active') ? $this.removeClass('active') : $this.addClass('active').siblings().removeClass('active');
						}).find('a').on('touchend', function (num) {
							return function () {
								core.href($(this).attr('href'));
							}
						}(i));
					}
					(data[i].list[0].type == 0) && $('html body .wrap .activity-list-map #map div div div:nth-child(5) .BMap_Marker').not($('html body .wrap .activity-list-map #map div div div:nth-child(5) .BMap_Marker').eq(0)).eq(i).find('img').addClass('blur');
				}
			}
		});
	}

	var bdMap = core.bdMap({
		selector: 'map',
		device: 'mb',
		map: true,
		gpsDetail: function (data) {		// 根据经纬度获取地址信息
			console.log(data);
			if ((data.result.location.lng != 0) && (data.result.location.lat != 0)) {
				$('html body .header-title h1, title').html(data.result.addressComponent.city);
				gpsDetail = data;
				lng = data.result.location.lng,
				lat = data.result.location.lat;
				addPoint(lng, lat);
			}
		},
		setMap: function (map, x, y) {
			if ((x == 0) || (y == 0)) {
				bdMap.netLocation();
			}
			if ((x != 0) && (y != 0)) {
				// 添加返回按钮
				$('#map').append('<div id="goBack" style="height: 32px; position: absolute; z-index: 1200; text-size-adjust: none; bottom: 30px; right: auto; top: auto; left: 10px;"><div style="height: 32px; margin: 0px; box-sizing: border-box; border: 1px solid #d9d7d5; border-radius: 3px; -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,.2); overflow: hidden;"><div style="float: left; width: 32px; height: 32px; background-image: url(http://api0.map.bdimg.com/images/geolocation-control/mobile/gradient-bg-1x64.png); background-size: 1px 32px; background-repeat: repeat-x;"><div style="width: 32px; height: 32px; cursor: pointer; background-image: url(http://api0.map.bdimg.com/images/geolocation-control/mobile/default-40x40.png); background-size: 20px 20px; background-repeat: no-repeat; background-position: center center;"></div></div></div></div>');
				$('#goBack').on('tap', function () {
					var nowPoint = new BMap.Point(lng, lat);
					bdMap.map.setZoom(14);
					map.setCenter(nowPoint);
				});

				// // 添加定位控件
				// var geolocationControl = new BMap.GeolocationControl();
				// geolocationControl.addEventListener("locationSuccess", function(e){
				// 	// 定位成功事件
				// 	addPoint(e.point.lng, e.point.lat);
				// });
				// geolocationControl.addEventListener("locationError",function(e){
				// 	// 定位失败事件
				// 	// alert(e.message);
				// });
				// map.addControl(geolocationControl);
			}
			$('body').on('touchend', function () {
				$('html body .wrap .activity-list-map #map div div div:nth-child(5) .BMap_Marker').not($('html body .wrap .activity-list-map #map div div div:nth-child(5) .BMap_Marker').eq(0)).css('zIndex', '1');
			});
			setTimeout(function () {
				$('body, .BMap_smcbg').on('touchend', function () {
					setTimeout(function () {
						$('html body .wrap .activity-list-map #map div div div:nth-child(5) .BMap_Marker').not($('html body .wrap .activity-list-map #map div div div:nth-child(5) .BMap_Marker').eq(0)).css('zIndex', '1');
					}, 25);
				});
			}, 1000);
		}
	}, useBaidu);
}
